package cn.lsnu.modules.oa.service;

import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.interfacetools.interfaceservice.BaseService;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.modules.oa.entity.RoleEntity;
import cn.lsnu.modules.oa.mapper.RoleMapper;
import cn.lsnu.utils.Global;
import cn.lsnu.utils.Guuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleService extends BaseService<RoleEntity,RoleMapper> {

    @Autowired
    private RoleMapper roleMapper;


    @Override
    public void save(RoleEntity entity) throws Exception {
        entity.setRoleId(Guuid.uuid());
        if(entity.getMenus() != null){
            List<MenuEntity> menuEntityLists = entity.getMenus();
            for(int i=0;i<menuEntityLists.size();i++){
                entity.setMenuId(menuEntityLists.get(i).getMenuId());

                if(entity.getMenuId() == null)
                    continue;
                /**
                 *排除没有查看权限的判断
                 */
                if(menuEntityLists.get(i).getLook() != null){
                    entity.setLook(menuEntityLists.get(i).getLook());
                    entity.setAdd(menuEntityLists.get(i).getAdd()==null?0:menuEntityLists.get(i).getAdd());
                    entity.setDel(menuEntityLists.get(i).getDel()==null?0:menuEntityLists.get(i).getDel());
                    entity.setUpd(menuEntityLists.get(i).getUpd()==null?0:menuEntityLists.get(i).getUpd());
                }
                roleMapper.insertRM(entity);
            }
        }
        super.save(entity);
    }

    @Override
    public RoleEntity get(String key) throws Exception {
        return super.get(key);
    }

    @Override
    public RoleEntity getEntity(RoleEntity entity) throws Exception {
        return super.getEntity(entity);
    }

    @Override
    public Page<RoleEntity> findPage(RoleEntity entity) throws Exception {
        Page<RoleEntity> page = super.findPage(entity);
        if(page == null)
            return page;
        List<RoleEntity> roleEntityList = page.getData();

        for(int i=0;i<roleEntityList.size();i++){
            String menuName = " ";
            if(roleEntityList.get(i).getMenus() == null){
                continue;
            }

            if(roleEntityList.get(i).getMenus().size() != 0){
                for (int j=0;j<roleEntityList.get(i).getMenus().size();j++){
                    menuName = menuName + roleEntityList.get(i).getMenus().get(j).getMenuName()+"  ";
                }
            }
            roleEntityList.get(i).setMenuName(menuName);

        }


        return page;
    }

    @Override
    public void delete(RoleEntity entity) throws Exception {
        roleMapper.deleteRM(entity);
        super.delete(entity);
    }

    @Override
    public void update(RoleEntity entity) throws Exception {
        if(entity.getMenus() != null){
            //权限不空进行删除
            roleMapper.deleteRM(entity);
            List<MenuEntity> menuEntityLists = entity.getMenus();
            for(int i=0;i<menuEntityLists.size();i++){
                entity.setMenuId(menuEntityLists.get(i).getMenuId());
                if(entity.getMenuId() == null)
                    continue;

                if(menuEntityLists.get(i).getLook() != null){
                    entity.setLook(menuEntityLists.get(i).getLook());
                    entity.setAdd(menuEntityLists.get(i).getAdd()==null?0:menuEntityLists.get(i).getAdd());
                    entity.setDel(menuEntityLists.get(i).getDel()==null?0:menuEntityLists.get(i).getDel());
                    entity.setUpd(menuEntityLists.get(i).getUpd()==null?0:menuEntityLists.get(i).getUpd());
                }else{
                    entity.setLook(0);
                    entity.setAdd(0);
                    entity.setDel(0);
                    entity.setUpd(0);
                }
                roleMapper.insertRM(entity);

            }
        }
        super.update(entity);
    }

    @Override
    public List<RoleEntity> findList(RoleEntity entity) throws Exception {
        return super.findList(entity);
    }

}
